package com.example.cardgame

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.GridLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private val EDIT_INFO_REQUEST_CODE = 1
    private lateinit var playerName: TextView
    private lateinit var playerGender: TextView
    private lateinit var playerPhone: TextView
    private lateinit var highScore: TextView



    lateinit var game: CardMatchingGame
    val cardButtons = mutableListOf<Button>()

    //声明 GridLayout, Button 和 TextView 变量
    private lateinit var gridLayout: GridLayout
    private lateinit var reset: Button
    private lateinit var score: TextView



//    @SuppressLint("MissingInflatedId")
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 使用 findViewById 获取视图
        gridLayout = findViewById(R.id.gridLayout) // 确保在布局文件中有这个 ID
        reset = findViewById(R.id.reset)  // 确保在布局文件中有这个 ID
        score = findViewById(R.id.score)    // 确保在布局文件中有这个 ID


        // 获取玩家信息视图
        playerName = findViewById(R.id.playerName)
        playerGender = findViewById(R.id.playerGender)
        playerPhone = findViewById(R.id.playerPhone)
        highScore = findViewById(R.id.highScore)

        // 设置示例玩家信息
        playerName.text = "姓名: 邹玉婷"
        playerGender.text = "性别: 女"
        playerPhone.text = "电话: 123456789"
        highScore.text = "当前最高分: 100"


        for (i in 0 until gridLayout.childCount) {
            val v = gridLayout.getChildAt(i)
            if (v is Button) {
                v.setOnClickListener(this)
                cardButtons.add(v)
            }
        }

        game = CardMatchingGame(cardButtons.count())
        updateUI()

        reset.setOnClickListener {
            game = CardMatchingGame(cardButtons.count())
            updateUI()
        }
    val editInfoButton: Button = findViewById(R.id.editInfoButton)
    editInfoButton.setOnClickListener {
        val intent = Intent(this, EditInfoActivity::class.java)
        startActivityForResult(intent, EDIT_INFO_REQUEST_CODE)
    }
}


    fun updateUI() {
        for (button in cardButtons) {
            val index = cardButtons.indexOf(button)
            val card = game.cardAtIndex(index)
            button.isEnabled = !card.isMatched
            if (card.isChosen) {
                button.text = card.toString()
                button.setBackgroundColor(Color.WHITE)
            } else {
                button.text = ""
                button.setBackgroundResource(R.drawable.lysk)
            }
        }
        score.text = String.format("%s%d", getString(R.string.Score), game.score)
    }

    override fun onClick(v: View?) {
        if (v is Button) {
            val index = cardButtons.indexOf(v)
            game.chooseCardAtIndex(index)
            updateUI()
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == EDIT_INFO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.let {
                playerName.text = "姓名: ${it.getStringExtra("name")}"
                playerGender.text = "性别: ${it.getStringExtra("gender")}"
                playerPhone.text = "电话: ${it.getStringExtra("phone")}"
            }
        }
    }
}


